//
//  WJ_Search_XiangQingVC.m
//  WJBSApp
//
//  Created by dllo on 16/7/17.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJ_Search_XiangQingVC.h"
#import "WJMeModel.h"
#import "WJMainModel.h"
#import "WJMainTableViewCell.h"
#import "WJEssAndNewPLVC.h"
#import "WJDataManager.h"
#import "WJ_TJDY_VC.h"


@interface WJ_Search_XiangQingVC () <UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) WJBaseButton *rightButton;

@property (nonatomic,retain) WJBaseTableView *tableView;

@property (nonatomic,retain) WJBaseImageView *headerImage;

@property (nonatomic,retain) WJBaseButton *rightBut;

@property (nonatomic,retain) UILabel *rightLable;

@property (nonatomic,retain) WJBaseButton *leftBut;

@property (nonatomic,retain) UILabel *leftLable;

@property (nonatomic,retain) UIScrollView *scrollView;

@property (nonatomic,retain) WJBaseTableView *rightTableView;

@property (nonatomic,retain) NSMutableArray *rightArray;

@property (nonatomic,retain) WJBaseTableView *leftTableView;

@property (nonatomic,retain) NSMutableArray *leftArray;

@property (nonatomic,retain) NSNumber *leftNp;

@property (nonatomic,retain) NSNumber *rightNp;

@end

@implementation WJ_Search_XiangQingVC




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.leftArray = [NSMutableArray array];
    
    self.rightArray = [NSMutableArray array];
    
    [self createConfig];
    
    [self createView];
        
}


- (void)createConfig
{
    // 影藏系统的返回按钮
    self.navigationItem.hidesBackButton = YES;
    
    self.view.backgroundColor = BSWhiteColor;
    self.navigationItem.title = self.model.theme_name;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationButtonReturn.png" hightImage:@"navigationButtonReturnClick.png" target:self action:@selector(searchXQLeftAction)];
    
    self.rightButton = [WJBaseButton buttonWithTitle:nil frame:CGRectMake(0, 0, 60, 30) target:self action:@selector(leftBarButtonItemAction:)];
    self.rightButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.rightButton setTitle:@"+订阅" forState:0];
    self.rightButton.layer.borderWidth = 1;
    self.rightButton.layer.cornerRadius = 3;
    
    BOOL  exist = [[WJDataManager shareFMDB] isExistTheme_id:self.model.theme_id];
    
    if (exist) {
        [self.rightButton setTitle:@"取消订阅" forState:0];
        [self.rightButton setTitleColor:BSGrayColor forState:0];
    }else{
        [self.rightButton setTitle:@"+订阅" forState:0];
        [self.rightButton setTitleColor:BSRedColor forState:0];
    }
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_rightButton];
    
}

// 放回上一个界面
- (void)searchXQLeftAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma Mark - 收藏的东西 点击添加订阅.点击进去第一个界面  那么问题来了 如何刷新Button SO ease
//   这里的归档与放归档肯定要写  不过无法刷新数据 所以本地数据也要操作
- (void)leftBarButtonItemAction:(WJBaseButton *)button
{
    if ([self.rightButton.currentTitle isEqualToString:@"取消订阅"]) {  //第一个数组删除
        
        [self.rightButton setTitle:@"+订阅" forState:0];
        [self.rightButton setTitleColor:BSRedColor forState:0];

        WJMeModel *model = self.model ;
        
        //添加数据库
        
        [[WJDataManager shareFMDB] deleteFromFirWithTheme_id:model.theme_id];
        [[WJDataManager shareFMDB] insertToSecTheme_id:model.theme_id Image_list:model.image_list Sub_number:model.sub_number Post_num:model.post_num Theme_name:model.theme_name];
        
    }else{ //第一个数组添加
        
        WJMeModel *model = self.model;
        
         //添加数据库
        [[WJDataManager shareFMDB] insertToFirTheme_id:model.theme_id Image_list:model.image_list Sub_number:model.sub_number Post_num:model.post_num Theme_name:model.theme_name];
        [[WJDataManager shareFMDB] deleteFromSecTheme_id:model.theme_id];
        
        [self.rightButton setTitle:@"取消订阅" forState:0];
        [self.rightButton setTitleColor:BSGrayColor forState:0];

    }
    
}



- (void)createView
{
    self.tableView = [[WJBaseTableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableViewCell"];
    
    // 两个按钮  和lable
    
    self.leftBut = [WJBaseButton buttonWithTitle:@"最热" frame:CGRectMake(0, -50, WIDTH / 2, 47) target:self action:@selector(leftAction:)];
    [self.tableView addSubview:self.leftBut];
    [self.leftBut setTitleColor:BSRedColor forState:0];
    self.leftLable = [[UILabel alloc]initWithFrame:CGRectMake(0, -3, WIDTH / 2, 2)];
    self.leftLable.backgroundColor = BSRedColor;
    [self.tableView addSubview:self.leftLable];
    
    
    self.rightBut = [WJBaseButton buttonWithTitle:@"最新" frame:CGRectMake(WIDTH / 2, -50, WIDTH / 2, 47) target:self action:@selector(rightAction:)];
    [self.tableView addSubview:self.rightBut];
    [self.rightBut setTitleColor:BSGrayColor forState:0];
    self.rightLable = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH / 2, -3, WIDTH / 2, 2)];
    self.rightLable.backgroundColor = BSRedColor;
    self.rightLable.hidden = YES;
    [self.tableView addSubview:self.rightLable];
    
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, -1, WIDTH, 1)];
    [self.tableView addSubview:lable];
    lable.backgroundColor = BSLightGrayColor;

    UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(0, -50, WIDTH, 1)];
    [self.tableView addSubview:lable1];
    lable1.backgroundColor = BSLightGrayColor;

    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.scrollView.backgroundColor = BSWhiteColor;
    [self.tableView addSubview:self.scrollView];
    self.scrollView.backgroundColor = BSOrangeColor;
    self.scrollView.contentSize = CGSizeMake(WIDTH * 2, 0);
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    
    
    self.leftTableView = [[WJBaseTableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.scrollView addSubview:self.leftTableView];
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;

    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.leftTableView registerClass:[WJMainTableViewCell class] forCellReuseIdentifier:@"leftTableCell"];
    
    
    self.rightTableView = [[WJBaseTableView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.scrollView addSubview:self.rightTableView];
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;

    self.rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.rightTableView registerClass:[WJMainTableViewCell class] forCellReuseIdentifier:@"rightTableCell"];
    
    
    
    //下拉刷新
    self.leftTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(createleftData)];
    
    self.rightTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(createRightData)];
    
    
    //根据下拉的距离提示符自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    //当已进入页面时自动下拉刷新
    [self.leftTableView.mj_header beginRefreshing];
    
    [self.rightTableView.mj_header beginRefreshing];
    
    //上拉加载以前的数据
    self.leftTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadLeftMoreData:self.leftNp];
    }];
    
    //上拉加载以前的数据
    self.rightTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadRightMoreData:self.rightNp];
    }];
    
}

- (void)leftAction:(WJBaseButton *)button
{
    [self.leftBut setTitleColor:BSRedColor forState:0];
    self.leftLable.hidden = NO;
    [self.rightBut setTitleColor:BSGrayColor forState:0];
    self.rightLable.hidden = YES;
    self.scrollView.contentOffset = CGPointMake(0, 0);
    
}

- (void)rightAction:(WJBaseButton *)button
{
    [self.leftBut setTitleColor:BSGrayColor forState:0];
    self.leftLable.hidden = YES;
    [self.rightBut setTitleColor:BSRedColor forState:0];
    self.rightLable.hidden = NO;
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:self.scrollView]) {
        NSInteger temp = scrollView.contentOffset.x / WIDTH;
        if (temp == 0) {
            [self.leftBut setTitleColor:BSRedColor forState:0];
            self.leftLable.hidden = NO;
            [self.rightBut setTitleColor:BSGrayColor forState:0];
            self.rightLable.hidden = YES;
            
        }else{
            [self.leftBut setTitleColor:BSGrayColor forState:0];
            self.leftLable.hidden = YES;
            [self.rightBut setTitleColor:BSRedColor forState:0];
            self.rightLable.hidden = NO;
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:self.leftTableView]) {
        
        return self.leftArray.count;
        
    }else if([tableView isEqual:self.rightTableView]) {
        
        return self.rightArray.count;
    }
    
    return 1;
}

//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:self.leftTableView]) {
        
        WJMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftTableCell"];
        
        WJMainModel *model = self.leftArray[indexPath.row];

        
//        cell.model = model;
        
        return cell;
        
    }else if([tableView isEqual:self.rightTableView]) {
        
        WJMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightTableCell"];
        
        WJMainModel *model = self.rightArray[indexPath.row];
    
            


        
        
//        cell.model = model;
        
        return cell;
        
    }
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.leftTableView]) {
        
        WJMainModel *model = self.leftArray[indexPath.row];
        
        return model.cellHeight;
        
    }else if([tableView isEqual:self.rightTableView]) {
        
        WJMainModel *model = self.rightArray[indexPath.row];
        
        return model.cellHeight;
        
    }
    
    return self.scrollView.frame.size.height - 64;
}



- (void)createleftData
{
    NSString *leftUrl = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%ld/hot/bs0315-iphone-4.3/0-20.json",(long)self.model.theme_id];
    
    [WJNetTool GET:leftUrl body:nil header:nil response:WJJSON success:^(id resuposeObject) {
        
        NSDictionary *dic = resuposeObject;
        NSArray *array = dic[@"list"];
        
        self.leftNp = dic[@"info"][@"np"];
        
        [self.leftArray removeAllObjects];
        
        for (NSDictionary *dic in array) {
            WJMainModel *model = [WJMainModel modelWithJSON:dic];
            [self.leftArray addObject:model];
        }
        
//        NSLog(@"%@",self.leftArray);
        [self.leftTableView reloadData];
        // 结束刷新
        [self.leftTableView.mj_header endRefreshing];
        
    } failure:^(NSError *error) {}];
}

- (void)createRightData{
    
    NSString *rightUrl = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%ld/new/bs0315-iphone-4.3/0-20.json",(long)self.model.theme_id];
    
    [WJNetTool GET:rightUrl body:nil header:nil response:WJJSON success:^(id resuposeObject) {
        NSDictionary *dic = resuposeObject;
        NSArray *array = dic[@"list"];
        
        self.rightNp = dic[@"info"][@"np"];
        
        [self.rightArray removeAllObjects];
        
        for (NSDictionary *dic in array) {
            WJMainModel *model = [WJMainModel modelWithJSON:dic];
            [self.rightArray addObject:model];
        }
        
        [self.rightTableView reloadData];
        // 结束刷新
        [self.rightTableView.mj_header endRefreshing];
        
    } failure:^(NSError *error) {}];

}




- (void)loadLeftMoreData:(NSNumber *)np
{
    [self.tableView.mj_header endRefreshing];
    
    NSString *leftUrl = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%ld/hot/bs0315-iphone-4.3/%@-20.json",(long)self.model.theme_id,np];
    
    [WJNetTool GET:leftUrl body:nil header:nil response:WJJSON success:^(id resuposeObject) {
        
        NSDictionary *dic = resuposeObject;
        NSArray *array = dic[@"list"];
        
        self.leftNp = dic[@"info"][@"np"];
        
        for (NSDictionary *dic in array) {
            WJMainModel *model = [WJMainModel modelWithJSON:dic];
            [self.leftArray addObject:model];
        }
        
        [self.leftTableView reloadData];
        
        [self.leftTableView.mj_footer endRefreshing];
        
    } failure:^(NSError *error) {}];
    
}




- (void)loadRightMoreData:(NSNumber *)np
{
    [self.tableView.mj_header endRefreshing];
    
    NSString *rightUrl = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%ld/new/bs0315-iphone-4.3/%@-20.json",(long)self.model.theme_id,np];
    
    [WJNetTool GET:rightUrl body:nil header:nil response:WJJSON success:^(id resuposeObject) {
        
        NSDictionary *dic = resuposeObject;
        NSArray *array = dic[@"list"];
        
        self.rightNp = dic[@"info"][@"np"];
        
        
        for (NSDictionary *dic in array) {
            WJMainModel *model = [WJMainModel modelWithJSON:dic];
            [self.rightArray addObject:model];
        }
        
        [self.rightTableView reloadData];
        
        [self.rightTableView.mj_footer endRefreshing];
        
    } failure:^(NSError *error) {}];
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
